//
//  HALoginVC.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HALoginVC.h"
#import "HAYHForgetPwdVC.h"
#import "HARegisterVC.h"
#import "HATabBarViewController.h"
#import "AppDelegate.h"

@interface HALoginVC ()

/***/
@property (nonatomic, strong) UITextField *phoneTextField;
/***/
@property (nonatomic, strong) UITextField *pwdTextField;
/***/
@property (nonatomic, strong) UIButton *forgetButton;
/***/
@property (nonatomic, strong) UIButton *loginButton;


@end

@implementation HALoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    self.phoneTextField.text = HAUserDefaults.mobile;

    
//    self.pwdTextField.text = @"123456";
    
    // Do any additional setup after loading the view.
}

-(void)loginButtonClick {
    if ([NSString isEmptyString:self.phoneTextField.text]) {
        [MBProgressHUD showError:@"请输入手机号"];
        return;
    }

    if ([NSString isEmptyString:self.pwdTextField.text]) {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"auth/login";
    request.parms = @{
        @"account":self.phoneTextField.text?:@"",
        @"password":self.pwdTextField.text?:@"",
        @"device":@"ios",
    };
    [MBProgressHUD showLoading:self.view];

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {

            [MBProgressHUD hideHUDForView:self.view];

            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                if ([[dataModel.data allKeys]containsObject:@"userinfo"]) {
                    HAUserInfoModel *userInfoModel = [HAUserInfoModel mj_objectWithKeyValues:[dataModel.data valueForKey:@"userinfo"]];
                    if ([[dataModel.data allKeys]containsObject:@"isLock"]) {
                        NSInteger isLock = [[dataModel.data valueForKey:@"isLock"] integerValue];
//                        isLock = 0;
                        userInfoModel.isLock = isLock;
                    }
                    [HAHelper keepUserInfo:userInfoModel];

                    [MBProgressHUD showSuccess:@"登录成功"];
                    [AppDelegate single].window.rootViewController = [[HATabBarViewController alloc]init];
                    
                }else {
                    [MBProgressHUD showError:@"登录失败"];
                }

            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

-(void)forgetButtonClick {
    HAYHForgetPwdVC *vc = [[HAYHForgetPwdVC alloc]init];
    vc.isLogin = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)rloginButtonClick {
    HARegisterVC *vc = [[HARegisterVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)setupUI {
    
    self.baseNavView.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:20 textColor:@"#000000" text:@"您好，欢迎回来" alignment:0];
    [self.view addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(app_topH+44+40);
    }];
    
    UIView *line1 = [UIView ha_createView];
    line1.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(titLabel.mas_bottom).offset(120);
        make.height.equalTo(@0.8);
    }];
    
    UIView *line2 = [UIView ha_createView];
    line2.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.top.mas_equalTo(line1.mas_bottom).offset(55);
        make.height.equalTo(@0.8);
    }];

    UIImageView *phoneImgView = [UIImageView ha_createImageView];
    phoneImgView.image = [UIImage imageNamed:@"ha_41"];
    [self.view addSubview:phoneImgView];
    [phoneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line1.mas_top).offset(-12);
        make.width.mas_offset(phoneImgView.image.size.width);
        make.height.mas_offset(phoneImgView.image.size.height);
    }];

    _phoneTextField = [[UITextField alloc]init];
    _phoneTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _phoneTextField.font = [UIFont systemFontOfSize:14];
    _phoneTextField.placeholder = @"请输入手机号";
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_phoneTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_phoneTextField];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneImgView);
        make.left.equalTo(phoneImgView.mas_right).offset(10);
        make.right.equalTo(line1);
        make.height.equalTo(@28);
    }];

    UIImageView *pwdImgView = [UIImageView ha_createImageView];
    pwdImgView.image = [UIImage imageNamed:@"ha_44"];
    [self.view addSubview:pwdImgView];
    [pwdImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.bottom.mas_equalTo(line2.mas_top).offset(-12);
        make.width.mas_offset(pwdImgView.image.size.width);
        make.height.mas_offset(pwdImgView.image.size.height);
    }];

    _pwdTextField = [[UITextField alloc]init];
    _pwdTextField.textColor = [UIColor colorWithHexString:@"#333333"];
    _pwdTextField.font = [UIFont systemFontOfSize:13];
    _pwdTextField.placeholder = @"请输入密码";
    _pwdTextField.secureTextEntry = YES;
    _pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_pwdTextField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:_pwdTextField];
    [self.pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(pwdImgView);
        make.left.equalTo(pwdImgView.mas_right).offset(10);
        make.right.equalTo(line2);
        make.height.equalTo(@28);
    }];

    _forgetButton = [[UIButton alloc]init];
    [_forgetButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_forgetButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    _forgetButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [_forgetButton setTitleColor:[UIColor colorWithHexString:@"#9E312E"] forState:UIControlStateNormal];
    [_forgetButton addTarget:self action:@selector(forgetButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_forgetButton];
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@80);
        make.height.equalTo(@28);
        make.top.equalTo(line2.mas_bottom).offset(18);
        make.right.equalTo(line2);
    }];


    _loginButton = [[UIButton alloc]init];
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _loginButton.titleLabel.font = [UIFont systemFontOfSize:16];
    _loginButton.backgroundColor = [UIColor colorWithHexString:@"#9E312E"];
    _loginButton.layer.cornerRadius = 4;
    _loginButton.layer.masksToBounds = true;
    [_loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(42);
        make.right.mas_equalTo(-42);
        make.top.mas_equalTo(line2.mas_bottom).offset(108);
        make.height.mas_offset(44);
    }];
    
    UIButton *rloginButton = [[UIButton alloc]init];
    [rloginButton setTitle:@"在线开通" forState:UIControlStateNormal];
    [rloginButton setTitleColor:[UIColor colorWithHexString:@"#9E312E"] forState:UIControlStateNormal];
    rloginButton.titleLabel.font = [UIFont systemFontOfSize:16];
    rloginButton.layer.cornerRadius = 4;
    rloginButton.layer.masksToBounds = YES;
    rloginButton.layer.borderColor = [UIColor colorWithHexString:@"#666666"].CGColor;
    rloginButton.layer.borderWidth = 0.6;
    [rloginButton addTarget:self action:@selector(rloginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rloginButton];
    [rloginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(42);
        make.right.mas_equalTo(-42);
        make.top.mas_equalTo(_loginButton.mas_bottom).offset(20);
        make.height.mas_offset(44);
    }];

    
}


@end
